package com.xiaoxi.bms.controller.common;

import com.xiaoxi.bms.common.constant.constant.CommonConstants;
import com.xiaoxi.bms.common.constant.constant.TimeConstants;
import com.xiaoxi.bms.common.constant.enumeration.statuscode.user.ValidateCodeUserEnums;
import com.xiaoxi.bms.common.response.ApiResponse;
import com.xiaoxi.bms.common.util.RedisUtils;
import com.xiaoxi.bms.common.util.UUIDUtils;
import com.xiaoxi.bms.domain.common.ImageCode;
import com.xiaoxi.bms.domain.common.vo.ImageCodeVO;
import com.xiaoxi.bms.service.common.verification.CodeImageService;
import io.lettuce.core.dynamic.annotation.Param;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * @Author xiaoxi
 * @ProjectName springcloudbms
 * @PackageName com.xiaoxi.codeimage.controller
 * @ClassName CodeImageController
 * @Description TODO
 * @Date 2023/2/2 19:34
 * @Version 1.0
 */
@RestController
@RequestMapping("/code/api/v1")
@Slf4j
public class CodeImageController {

    @Resource
    private CodeImageService codeImageService;

    @Resource
    private RedisUtils redisUtils;

    @GetMapping("getVerificationCode")
    public ApiResponse createCode() {
        ImageCode imageCode = null;
        try {
            imageCode = codeImageService.createImageCode();
        } catch (IOException ioException) {
            log.error("图片验证码出现异常：{}", ioException.getMessage());
            return ApiResponse.ofSuccess();
        }
        String uuid = UUIDUtils.getRandomUUID();
        redisUtils.set(CommonConstants.IMAGE_CODE + uuid, imageCode, TimeConstants.VALIDATE_CODE);
        log.info("uuid : {} ---- 验证码 ： {}", uuid, imageCode.getCode());
        return ApiResponse.ofSuccess(new ImageCodeVO(imageCode.getImage(), uuid));
    }

}
